"use strict";
const BaseImpl = require("../basics/clz/baseimpl");
const Constant = require("../basics/constant");
const DicsModel = require("../model/dics");
const MasterModel = require("../model/master");
/**
 * 通用服务
 */
class HomeImpl extends BaseImpl {
  /**
   * 查询用户
   * @param {*} params 
   */
  async queryMasterList(params) {
    let { pageIndex, pageSize, isband, openId, userNick } = params;
    const masterModel = MasterModel.getInstant(this.cloud);
    const ex = masterModel.createExample();
    if (userNick != "tpmorefun") {
      ex.criteria.andEqual("openId", openId);
    }
    if (isband == Constant.YesOrNo.yes) {
      ex.criteria.andNotEqual("appId", "");
    }
    ex.setPage(pageIndex, pageSize);
    return await masterModel.selectPageList(ex);
  }

  /**
   * 查询用户
   * @param {*} params 
   */
  async initMaster(params) {
    let { openId, userNick, accessToken } = params;
    const masterModel = MasterModel.getInstant(this.cloud);
    let master = await masterModel.selectOneByOpenId(openId);
    if (!master) {
      await masterModel.insertOne({ openId, userNick, session: accessToken, shopId: "", appId: "", sellerId: "", shopUrl: "", memberurl: "" });
      return true;
    }
    let t = { session: accessToken, userNick };
    await masterModel.updateById(t, master.id);
    return true;
  }

  /**
   * 查询用户
   * @param {*} params 
   */
  async editMaster(params) {
    let { id, shopId, sellerId } = params;
    await MasterModel.getInstant(this.cloud).updateById({ shopId, sellerId }, id);
    return true;
  }

  /**
   * 更新app
   * @param {*} params 
   */
  async editAppId(params) {
    let { userId, appId } = params;
    await MasterModel.getInstant(this.cloud).updateById({ appId }, userId);
    return true;
  }

  /**
   * 查询labels
   * @param {*} params 
   */
  async quereyMasterLabels(params) {
    let { openId, userNick } = params;
    const masterModel = MasterModel.getInstant(this.cloud);
    const ex = masterModel.createExample();
    if (userNick != "tpmorefun") {
      ex.criteria.andEqual("openId", openId);
    }
    ex.setColumns(["id value", "userNick label", "session"]);
    return await masterModel.selectListByExample(ex);
  }

  /**
   * 查询labels
   * @param {*} params 
   */
  async quereyAppLabels(params) {
    // let { openId, userNick } = params;
    const masterModel = MasterModel.getInstant(this.cloud);
    const ex = masterModel.createExample();
    ex.criteria.andNotEqual("appId", "");
    ex.setColumns(["id value", "userNick label", "session"]);
    return await masterModel.selectListByExample(ex);
  }

  async queryLiveCardData(params) {
    let { id } = params;
    return await DicsModel.getInstant(this.cloud).selectStrOne(Constant.DICS.LIVE_CARD_DATA + id);
  }

  async editLiveCardData(params) {
    let { id, ctxt } = params;
    const dicsModel = DicsModel.getInstant(this.cloud);
    const ex = dicsModel.createExample();
    ex.criteria.andEqual("code", Constant.DICS.LIVE_CARD_DATA + id);
    const data = await dicsModel.selectOneByExample(ex);
    if (data) {
      await dicsModel.updateById({ ctxt }, data.id);
    } else {
      await dicsModel.insertOne({ name: "小部件状态同步", code: Constant.DICS.LIVE_CARD_DATA + id, ctxt, isuse: 1 });
    }
    return true;
  }

  async executeSql(params){
    let { sql } = params;
    return await DicsModel.getInstant(this.cloud).mfSql(sql,[]);
  }
}

module.exports = HomeImpl;